package com.qfedu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.dao.StaffDao;
import com.qfedu.entity.*;
import com.qfedu.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    private StaffDao staffDao;

    @Override
    public boolean addStaff(Staff staff) {

        int i = staffDao.addStaff(staff);
        /**
         * 修改部门名字  根据部门id
         */

        if (i > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteStaffByuNo(String uNo) {
        int s = staffDao.deleteStaffByuNo(uNo);
        if(s > 0){

            return true;

        }
        return false;
    }

    @Override
    public ResponseData<List<Staff>> findAllStaff(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Staff> staffList = staffDao.findAllStaff();
        PageInfo pageInfo = new PageInfo(staffList);

        int count = (int) pageInfo.getTotal();

        ResponseData<List<Staff>> responseData = new ResponseData<>();

        responseData.setCode(0);
        responseData.setData(staffList);
        responseData.setCount(count);
        responseData.setMsg("ok");

        return responseData;
    }

    @Override
    public DepartNameWithStaffNum getDepartNameWithStaffNum() {
        List<Map> departNameStaffNumMap = staffDao.getDepartNameWithStaffNum();
        List<String> departNameList = new ArrayList<>();
        List<Integer> staffNumList = new ArrayList<>();
//        gradeNameList.add((String)map.get("name"));
//        Long num1=(Long)map.get("num");
//        studentNumList.add(num1.intValue());
        for (Map map :
                departNameStaffNumMap) {
            departNameList.add((String) map.get("name"));

            Long num = (Long) map.get("num");
            staffNumList.add(num.intValue());
        }
        DepartNameWithStaffNum departNameWithStaffNum = new DepartNameWithStaffNum();
        departNameWithStaffNum.setStaffNumList(staffNumList);
        departNameWithStaffNum.setDepartNameList(departNameList);
        return departNameWithStaffNum;
    }

    public StaffNumDate getStaffNumData(){
        List<Map> staffNum = staffDao.getStaffNum();

        List<String> staffNameList = new ArrayList<>();
        List<Integer> staffNumList = new ArrayList<>();

        for (Map map:staffNum){
            staffNameList.add((String) map.get("name"));
            Long lNum = (Long) map.get("num");

            staffNumList.add(lNum.intValue());
        }

        StaffNumDate staffNumDate = new StaffNumDate();
        staffNumDate.setStaffNumList(staffNumList);
        staffNumDate.setStaffNameList(staffNameList);

        return staffNumDate;

    }
}
